﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Exentials.Xoox
{
    /// <summary>
    /// Implement a list of XElementBase with a standard Count attribute
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class XElementList<T> : XElementBase, IEnumerable<T> where T : XElementBase
    {
        private XAttributeBase<int> _count;
        private List<T> _items;

        public XElementList(string tagName)
            : base(tagName)
        {
            _count = new XAttributeBase<int>(this, "count") { Value = 0 };
            _items = new List<T>();
        }

        public int Add(T content)
        {
            _items.Add(content);
            base.Add(content);
            return _count.Value++;
        }

        public T this[int index]
        {
            get
            {
                return this._items[index];
            }
        }

        public int IndexOf(T item)
        {
            return this._items.IndexOf(item);
        }

        public int Count
        {
            get { return _count.Value; }
        }

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            return this._items.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this._items.GetEnumerator();
        }

        #endregion
    }
}
